

import { InputNumber, InputNumberProps } from 'antd';
import './forms.scss'
import { useFormItemInit } from './hooks';
import { FormItemBaseProps } from './interface';
import React from 'react';
import { useDrag } from './drop.hooks';
import Readonly from './Readonly';
import { Utils } from '../../utils/utils';
import { Components } from '../..';




interface IProps extends FormItemBaseProps {
}
export default function MyNumber(props: IProps) {
    const { formControl, className = '', style = {} } = props;
    const {
        value, setValue,
        readonly,
        outerClassName,
        layout,
        showError,
        outerStyle,
        labelStyle,
        selectorStyle,
    } = useFormItemInit(formControl, className, style);


    React.useEffect(() => {
    }, [readonly]);

    const onInputChange = (value: number | string | null) => {
        value = value ?? null;
        if (Utils.isString(value)) {
            value = +(value as string);
        }
        setValue(value);
        formControl.setValue(value);
    };

    const onFocus = () => {
    };

    const onBlur = () => {
    };

    const inputProps: InputNumberProps = {
        onChange: onInputChange,
        prefix: formControl.controlData.prefix,
        addonAfter: formControl.controlData.suffix ?? undefined,
        size: formControl.controlData.size ?? 'middle', // large | middle | small
        className: 'number',
        controls: false,
        value: value,
        placeholder: formControl.controlData.placeholder || '请输入',
        maxLength: formControl.controlData.inputMaxlength,
        disabled: readonly,
        status: showError ? 'error' : undefined,
        onFocus,
        onBlur,
        style: selectorStyle,
    }

    /** 拖拽排序 */
    const dropRef = React.useRef<HTMLDivElement>(null);
    useDrag({ dropRef, formControl });

    return (
        <div component-name='form-item' className={outerClassName} style={outerStyle} ref={dropRef}>
            <div className={`field ${layout}`}>
                <Components.forms.FormLabel formControl={formControl} style={labelStyle} />
                <div className='control'>
                    <div className="value number-value">
                        {
                            readonly ? <Readonly formControl={formControl} /> : <InputNumber {...inputProps} />
                        }
                    </div>
                </div>
            </div>
        </div>
    )
}
